package com.yanqu.road.dao.impl.drugstore;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.UserDrugLandData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDrugLandDataDaoImpl extends TempDataObjectDao<UserDrugLandData> {

    public static String getUserDrugLandDataSql = "select * from `t_u_drug_land` where `userId`=?";
    public List<UserDrugLandData> getUserDrugLandData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserDrugLandData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDrugLandDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertUserDrugLandDataSql = "insert into `t_u_drug_land`(userId,landId,drugId,state,plantTime,matureTime,lastMatureTime,planId,orderNo) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserDrugLandData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLandId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrugId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlantTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMatureTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMatureTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlanId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOrderNo()));
        if (getDb().executeNoneQuery(insertUserDrugLandDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDrugLandDataSql = "update `t_u_drug_land` set drugId=?,state=?,plantTime=?,matureTime=?,lastMatureTime=?,planId=?,orderNo=? where userId=? and landId=?";

    @Override
    protected boolean updateData(UserDrugLandData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrugId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlantTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMatureTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMatureTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlanId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOrderNo()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLandId()));
        if (getDb().executeNoneQuery(updateUserDrugLandDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserDrugLandData parseData(ResultSet rs) throws SQLException {
        UserDrugLandData data = new UserDrugLandData();
        data.setUserId(rs.getLong("userId"));
        data.setLandId(rs.getInt("landId"));
        data.setDrugId(rs.getInt("drugId"));
        data.setState(rs.getInt("state"));
        data.setPlantTime(rs.getLong("plantTime"));
        data.setMatureTime(rs.getLong("matureTime"));
        data.setLastMatureTime(rs.getLong("lastMatureTime"));
        data.setPlanId(rs.getLong("planId"));
        data.setOrderNo(rs.getLong("orderNo"));

        return data;
    }
}
